package com.sdwl.brcl.validation.validation;

import org.apache.commons.lang3.StringUtils;

/**
 * @author xied
 * @date 2021年06月11日 14:13
 */
public class StringValidator implements Validator {
    public static final int NOT_BLANK = 0;
    public static final int NOT_EMPTY = 1;
    public static final int EQ = 2;
    public static final int EQ_IGNORE_CASE = 3;
    public static final int MIN_LENGTH = 4;
    public static final int MAX_LENGTH = 5;
    public static final int EQ_LENGTH = 6;
    public static final int NOT_EQ = 7;
    private int type;

    public StringValidator(int type) {
        this.type = type;
    }

    @Override
    public boolean validate(Object value) {
        if (this.type == 0) {
            return StringUtils.isNotBlank((String) value);
        } else if (this.type == 1) {
            return StringUtils.isNotEmpty((String) value);
        } else {
            Object[] values;
            if (this.type == 2) {
                values = (Object[]) ((Object[]) value);
                return StringUtils.equals((String) values[0], (String) values[1]);
            } else if (this.type == 3) {
                values = (Object[]) ((Object[]) value);
                return StringUtils.equalsIgnoreCase((String) values[0], (String) values[1]);
            } else if (this.type == 4) {
                values = (Object[]) ((Object[]) value);
                return StringUtils.length((String) values[0]) >= (Integer) values[1];
            } else if (this.type == 5) {
                values = (Object[]) ((Object[]) value);
                return StringUtils.length((String) values[0]) <= (Integer) values[1];
            } else if (this.type == 6) {
                values = (Object[]) ((Object[]) value);
                return StringUtils.length((String) values[0]) == (Integer) values[1];
            } else if (this.type == 7) {
                values = (Object[]) ((Object[]) value);
                return !StringUtils.equals((String) values[0], (String) values[1]);
            } else {
                throw new UnsupportedOperationException("不支持的字符串操作");
            }
        }
    }

    @Override
    public String validateCode() {
        if (this.type == 0) {
            return "not.blank";
        } else if (this.type == 1) {
            return "not.empty";
        } else if (this.type == 2) {
            return "must.eq";
        } else if (this.type == 3) {
            return "must.eq.ignore.case";
        } else if (this.type == 4) {
            return "min.length";
        } else if (this.type == 5) {
            return "max.length";
        } else {
            return this.type == 7 ? "not.eq" : "";
        }
    }

    @Override
    public String validateMsg(Object value, String validateName) {
        if (this.type == 0) {
            return validateName + "不能为空";
        } else if (this.type == 1) {
            return validateName + "不能为空";
        } else if (this.type == 2) {
            return validateName + "不相同";
        } else if (this.type == 3) {
            return validateName + "不相同";
        } else {
            Object[] values;
            if (this.type == 4) {
                values = (Object[]) ((Object[]) value);
                return validateName + "允许最小长度为" + values[1];
            } else if (this.type == 5) {
                values = (Object[]) ((Object[]) value);
                return validateName + "允许最大长度为" + values[1];
            } else if (this.type == 7) {
                values = (Object[]) ((Object[]) value);
                return validateName + "不能等于" + values[1];
            } else {
                return "";
            }
        }
    }
}
